package com.dky.satell.util;


import cn.hutool.http.HttpRequest;
import com.dky.satell.entity.xml.FileSchema;
import lombok.extern.slf4j.Slf4j;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;


@Slf4j
public class ServiceUtil {

    public static FileSchema analysisXml(File file) {
        //利用输入流获取XML文件内容
        try {
            if(!file.exists()) {
                return new FileSchema();
            }
            //利用JAXBContext将类转为一个实例
            JAXBContext context = JAXBContext.newInstance(FileSchema.class);
            //XMl 转为对象的接口
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Object xmlObject = unmarshaller.unmarshal(new FileInputStream(file));
            return (FileSchema) xmlObject;
        } catch (Exception e) {
            log.error("解析xml文件[{}]失败", file.getName(), e);
            return new FileSchema();
        }
    }

    public static void writeXml(FileSchema fileSchema, String path) {
        try {
            JAXBContext context = JAXBContext.newInstance(FileSchema.class);
            // 创建 Marshaller 实例
            Marshaller marshaller = context.createMarshaller();
            // 设置转换参数 -> 这里举例是告诉序列化器是否格式化输出
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            //省略掉 xml 头部的 standalone
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
            // 构建输出环境 -> 这里使用标准输出，输出到 path
            OutputStream out = new FileOutputStream(path);
            // 将所需对象序列化 -> 该方法没有返回值
            marshaller.marshal(fileSchema, out);
            out.close();
        } catch (Exception e) {
            log.error("文件[{}]生成xml失败", fileSchema.getFileName());
        }
    }

    public static void sendToProxy(String fileName, long size, String region, String proxyFileSend) {
        log.info("请求代理生成xml文件[{}]", fileName + ".xml");
//        HttpRequest.get(proxyFileSend)
//                .form("fileName", fileName)
//                .form("destZoneId", region)
//                .form("fileSize", size)
//                .execute();
    }

    public static String buildMetaName(String fileName) {
        int index = fileName.lastIndexOf(".");
        return fileName.substring(0, index + 1) + "meta";
    }

    public static String buildProxyFileName(String fileName, String metaName) {
        int fileIndex = fileName.lastIndexOf(".");
        int metaIndex = metaName.lastIndexOf(".");
        return metaName.substring(0, metaIndex + 1) + fileName.substring(fileIndex + 1);
    }

    public static String getFileNameByPath(String path) {
        int index = path.lastIndexOf(File.separator);
        return path.substring(index + 1);
    }

    public static String buildFileDirectory(String rootPath, String path) {
        path = path.substring(rootPath.length());
        int index = path.lastIndexOf(File.separator);
        return path.substring(0, index);
    }

}
